package com.yunhe.sms.service.activity.impl;

import com.yunhe.commons.dto.Pageable;
import com.yunhe.commons.util.IPUtils;
import com.yunhe.commons.util.StringUtils;
import com.yunhe.commons.util.TimeUtils;
import com.yunhe.commons.util.UdpGetClientMacAddr;
import com.yunhe.sms.dao.activity.VoteItemDao;
import com.yunhe.sms.dao.activity.VoteLogDao;
import com.yunhe.sms.dao.activity.impl.VoteItemDaoImpl;
import com.yunhe.sms.dao.activity.impl.VoteLogDaoImpl;
import com.yunhe.sms.entity.activity.Vote;
import com.yunhe.sms.entity.activity.VoteItem;
import com.yunhe.sms.entity.activity.VoteLog;
import com.yunhe.sms.service.activity.VoteItemService;
import com.yunhe.sms.service.activity.VoteService;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/10/30 0030.
 */
public class VoteItemServiceImpl implements VoteItemService {
    private VoteItemDao voteItemDao = new VoteItemDaoImpl();
    private VoteService voteService = new VoteServiceImpl();
    private VoteLogDao voteLogDao = new VoteLogDaoImpl();
    @Override
    public void save(VoteItem voteItem) {
        voteItemDao.save(voteItem);
    }

    @Override
    public void delete(Long id) {
        voteItemDao.delete(id);
    }

    @Override
    public void deleteBatch(String ids) {
        if(StringUtils.notEmpty(ids)){
            String[] idArr = ids.split(",");
            for(String i:idArr){
                this.delete(new Long(i));
            }
        }
    }

    @Override
    public void update(VoteItem voteItem) {
        voteItemDao.update(voteItem);
    }

    @Override
    public void updateVotes(Long id) {
        voteItemDao.updateVotes(id);
    }

    @Override
    public VoteItem find(Long id) {
        return voteItemDao.find(id);
    }

    @Override
    public List<VoteItem> findList(Long voteId) {
        return voteItemDao.findList(voteId);
    }

    @Override
    public Pageable<VoteItem> findPage(Integer offset, Integer limit, Long voteId) {
        return voteItemDao.findPage(offset,limit,voteId);
    }


    @Override
    public VoteLog mac(Long id,HttpServletRequest req) {

        String ip = IPUtils.getIp(req);
        UdpGetClientMacAddr umac = new UdpGetClientMacAddr(ip);
        String mac = umac.GetRemoteMacAddr();
        VoteLog voteLog = new VoteLog(ip, mac, new Date(), id);

        return voteLog;

    }

    @Override
    public Boolean toupiao(Long id,HttpServletRequest req) {

        Boolean flag = false;

        Vote vote = voteService.find(id);
        Long nowTime = new Date().getTime();
        Long startTime = vote.getStartTime().getTime();
        Long endTime = vote.getEndTime().getTime();
        if(nowTime<startTime || nowTime >endTime){
            flag = true;
        }


        String day = TimeUtils.format(new Date(), TimeUtils.FORMAT_YYYY_MM_DD);
        VoteLog voteLog = this.mac(id, req);
        List<VoteLog> list = voteLogDao.fingList(voteLog.getVoteId(),voteLog.getMac(),day);  //判断今天是否投过票

        if(null!= list && list.size()>0){
            flag = true;
        }

        return flag;
    }
}
